#!/bin/bash
# ============================================================
# Script Name:    agvBringup
# Description:    AGV系统ROS2主启动脚本
# Author:         haibo
# Created Date:   2025-09-10
# Version:        1.0
# ============================================================
LOG_FILE="${1:-/tmp/agv_bringup.log}"

export PATH="${HOME}/lgimRobot/bin:$PATH"
source /opt/ros/humble/setup.bash
source ${HOME}/lgimRobot/setup.bash
source ${HOME}/lgimRobot/bin/common "$LOG_FILE"

MODULES=(
    "peripheralBringup"
    "lidarBringup"
    "cameraBringup"
    "localizationBringup"
    "imuBringup"
    "auxSystemBringup"
    "webBringup"
    "toolsBringup"
)

main() {
    log "INFO" "===== AGV ROS2系统启动开始 ====="
    cleanup_processes
    if ! result=$(baseBringup "$LOG_FILE"); then
        log "ERROR" "baseBringup 脚本执行失败"
        exit 1
    fi

    if [ -z "$result" ]; then
        log "ERROR" "baseBringup 脚本未返回任何参数，请检查其实现"
        exit 1
    fi

    local params="$result"

    # 启动所有模块
    for module in "${MODULES[@]}"; do
        "${module}" "$LOG_FILE" "$params"
        local status=$?
        
        if [ $status -ne 0 ]; then
            log "ERROR" "模块启动失败: $module (错误码: $status)"
        fi
        sleep 1
    done
    
    log "INFO" "===== AGV ROS2系统启动完成 ====="
    return 0
}

# trap cleanup_processes EXIT
main